#! /usr/bin/evn python
# coding:utf-8

from data import DB
from pycalendar import *
import uuid
import  datetime


class QComboBox(QComboBox):

    def wheelEvent(self, QWheelEvent):
        pass

class QDateTimeEdit(QDateTimeEdit):

    def wheelEvent(self, QWheelEvent):
        pass

class Travel(QWidget):

    def __init__(self,uuid):
        super(Travel, self).__init__()
        self.setWindowTitle("旅行史")
        self.setFixedSize(300,420)
        self.setWindowFlags(Qt.WindowMinimizeButtonHint)
        self.center()
        self.db = DB()
        self.cur = self.db.cur
        self.uuid = uuid
        self.set_ui()

    def set_ui(self):
        self.save_bnt = QPushButton("保存(F1)")
        self.save_bnt.clicked.connect(self.save_record)
        self.save_bnt.setStyleSheet("width:40px;height:30px;color:white;background-color:green;border:hidden;")
        self.new_bnt = QPushButton("添加(F2)")
        self.new_bnt.clicked.connect(self.new_record)
        self.new_bnt.setStyleSheet("width:40px;height:30px;color:white;background-color:brown;border:hidden;")
        self.close_bnt = QPushButton("关闭(ESC)")
        self.close_bnt.clicked.connect(self.close_win)
        self.close_bnt.setStyleSheet("width:40px;height:30px;color:white;background-color:red;border:hidden;")
        self.history_bnt = QPushButton("行程")
        self.history_bnt.clicked.connect(self.show_history)
        self.history_bnt.setStyleSheet("width:40px;height:30px;color:white;background-color:blue;border:hidden;")
        bnt_widget = QWidget()
        bnt_layout = QHBoxLayout()
        bnt_layout.addWidget(self.close_bnt)
        # bnt_layout.addWidget(self.history_bnt)
        bnt_layout.addWidget(self.new_bnt)
        bnt_layout.addWidget(self.save_bnt)
        bnt_widget.setLayout(bnt_layout)

        self.begin_date_label = QLabel("出发日期")
        self.begin_date = QDateTimeEdit()
        self.begin_date.setStyleSheet("width:60px;height:30px;color:black;background-color:white;border:hidden;")
        self.begin_date.setCalendarPopup(True)
        self.begin_date.setDateTime(QDateTime.currentDateTime())
        self.begin_date_bnt = QPushButton()
        self.begin_date_bnt.setStyleSheet("border:hidden;")
        self.begin_date_bnt.setIcon(QIcon('./img/cal.ico'))
        self.begin_date_bnt.clicked.connect(lambda:self.show_dt_cal(self.begin_date))
        self.end_date_label = QLabel('停留时间')
        # self.end_date = QDateTimeEdit()
        self.end_date = QLineEdit()
        # self.end_date.setCalendarPopup(True)
        self.end_date.setStyleSheet("width:60px;height:30px;color:black;background-color:white;border:hidden;")
        # self.end_date.setDateTime(QDateTime.currentDateTime())
        # self.end_date_bnt = QPushButton()
        # self.end_date_bnt.setStyleSheet("border:hidden;")
        # self.end_date_bnt.setIcon(QIcon('./img/cal.ico'))
        # self.end_date_bnt.clicked.connect(lambda:self.show_dt_cal(self.end_date))

        self.from_address = QLineEdit()
        self.from_address.setStyleSheet("width:60px;height:30px;color:black;background-color:white;border:hidden;")
        self.from_gps = QLineEdit()
        self.from_gps.setStyleSheet("width:60px;height:30px;color:black;background-color:white;border:hidden;")

        self.to_address = QLineEdit()
        self.to_gps = QLineEdit()
        self.to_address.setStyleSheet("width:60px;height:30px;color:black;background-color:white;border:hidden;")
        self.to_gps.setStyleSheet("width:60px;height:30px;color:black;background-color:white;border:hidden;")

        self.vehicle = QLineEdit()
        self.vehicle.setStyleSheet("width:60px;height:30px;color:black;background-color:white;border:hidden;")

        self.risk = QComboBox()
        self.risk.setStyleSheet("width:60px;height:30px;color:black;background-color:white;border:hidden;")
        risk_list = ['是','否']
        for i in risk_list:
            self.risk.addItem(i)
        self.backup = QLineEdit()
        self.backup.setStyleSheet("width:60px;height:30px;color:black;background-color:white;border:hidden;")

        self.travel_uuid = QLabel(self.gen_uuid())
        self.travel_uuid.setStyleSheet("width:60px;height:30px;color:black;background-color:None;border:hidden;")

        layout = QGridLayout()
        # layout.addWidget(QLabel("+++++旅行史+++++"),0,0,1,2)
        layout.addWidget(QLabel("出发地"),1,0)
        layout.addWidget(self.from_address,1,1)
        layout.addWidget(QLabel("出发地GPS"),2,0)
        layout.addWidget(self.from_gps,2,1)
        layout.addWidget(self.begin_date_label,3,0)
        layout.addWidget(self.begin_date,3,1)
        layout.addWidget(self.begin_date_bnt,3,2)
        layout.addWidget(QLabel("目的地"),4,0)
        layout.addWidget(self.to_address,4,1)
        layout.addWidget(QLabel("目的地GPS"),5,0)
        layout.addWidget(self.to_gps,5,1)
        layout.addWidget(self.end_date_label,6,0)
        layout.addWidget(self.end_date,6,1)
        layout.addWidget(self.end_date_bnt,6,2)
        layout.addWidget(QLabel("交通工具"),7,0)
        layout.addWidget(self.vehicle, 7, 1)
        layout.addWidget(QLabel("同行者"),8,0)
        layout.addWidget(self.co_traveller,8,1)
        layout.addWidget(QLabel("备注"),9,0)
        layout.addWidget(self.backup,9,1)

        layout.addWidget(bnt_widget,10,0,1,3)
        layout.addWidget(self.travel_uuid,12,0,1,3)
        self.setLayout(layout)

    def save_record(self):
        data_dic = {
            'uuid':self.uuid,
            'date_from':self.change_datetime(self.begin_date),
            'from':self.from_address.text(),
            'gps_from':self.from_gps.text(),
            'to':self.to_address.text(),
            'gps_to':self.to_gps.text(),
            'date_to':self.change_datetime(self.end_date),
            'vehicle': self.vehicle.text(),
            'co_traveller': self.co_traveller.text(),
            'backup': self.backup.text(),
            'travel_uuid':self.travel_uuid.text()
        }
        if self.from_address.text():
            # if self.save_bnt.text() == '保存':
            try:
                sql = '''insert into travel values ("{travel_uuid}","{uuid}",{date_from},
                        "{from}","{gps_from}",{date_to},"{to}","{gps_to}",
                        "{vehicle}","{co_traveller}","{backup}")
                    '''.format(**data_dic)
                self.cur.execute(sql)
                # self.save_bnt.setText("更新")
            # else:
            except:
                sql = '''update travel set date_from = {date_from},
                        from_address = "{from}",gps_from = "{gps_from}",date_to = {date_to},
                        to_address = "{to}",gps_to = "{gps_to}",vehicle = "{vehicle}",
                        co_traveller = "{co_traveller}",backup = "{backup}" where
                        travel_uuid = "{travel_uuid}"
                    '''.format(**data_dic)
                self.cur.execute(sql)
            self.db.con.commit()

    def new_record(self):
        self.travel_uuid.setText(self.gen_uuid())
        # self.save_bnt.setText('保存(F1)')
        self.from_address.clear()
        self.from_gps.clear()
        self.to_address.clear()
        self.to_gps.clear()
        self.vehicle.clear()
        self.co_traveller.clear()
        self.backup.clear()

    def close_win(self):
        self.db.con.close()
        self.close()

    def show_dt_cal(self, tmp):#含时间日历控件
        self.cal = Calendar_With_Clock(tmp)
        self.cal.show()
        self.cal.datetime_signal.connect(self.input_deathday)

    def input_deathday(self, list):
        list[0].setDateTime(list[1])

    def change_datetime(self,pyqtdatetime):   # a pyqtdate style /yyyymmdd to time stamp
        pydate = pyqtdatetime.dateTime().toPyDateTime()
        base_date = datetime.datetime(1970,1,1,0,0)
        day_delta = pydate - base_date
        days = day_delta.days
        seconds = day_delta.seconds
        seconds += days*24*3600
        return seconds

    def show_history(self):
        self.win = Travel_History(self.uuid)
        self.win.show()

    def gen_uuid(self):
        return (str(uuid.uuid1()))

    def center(self):
        self.screen = QDesktopWidget().screenGeometry()
        self.size = self.geometry()
        self.move(int((self.screen.width()-self.size.width())/2),
                 int((self.screen.height()-self.size.height())/2)
                  )

    def keyPressEvent(self,e):
        if e.key() == Qt.Key_Escape:
            self.close_win()
        if e.key() == Qt.Key_F1:
            self.save_record()
        if e.key() == Qt.Key_F2:
            self.new_record()

class Travel_History(QWidget):

    def __init__(self,uuid):
        super(Travel_History,self).__init__()
        self.uuid = uuid
        self.setWindowTitle("旅行史一览表")
        self.setFixedSize(900,500)
        self.setWindowFlags(Qt.WindowMinimizeButtonHint)
        self.center()
        self.db = DB()
        self.cur = self.db.cur
        sql = '''select location.* from location where
            location.uuid = "{}" order by date'''.format(self.uuid)
        self.cur.execute(sql)
        self.rslt = self.cur.fetchall()
        self.win = None
        self.set_ui()

    def set_ui(self):

        count = len(self.rslt)
        self.table = QTableWidget(count,7) #15rows 15columns
        table_width_list = [50,120,200,100,100,120,150]
        for i in range(7):
            self.table.setColumnWidth(i,table_width_list[i])

        self.table.setHorizontalHeaderLabels(['操作','场所名称','场所地址','时间','停留时长','是否有风险','备注'])
        k = 0  # k 行数
        for i in self.rslt:
            if i:
                tmp = [i[j] for j in [3,4,5,6,7,8]]
                tmp[2] = self.to_date(tmp[2])
                for j in range(6): #15columns 第一列为操作列
                    item_tmp = QTableWidgetItem(str(tmp[j]))
                    item_tmp.setTextAlignment(Qt.AlignCenter)
                    self.table.setItem(k,j+1,item_tmp)
                self.table.setCellWidget(k,0,self.button_row(i[0]))
            else:
                break
            k += 1

        new_travel_bnt = QPushButton("添加行程")
        new_travel_bnt.setStyleSheet("width:40px;height:30px;color:white;background-color:green;border:hidden;")
        new_travel_bnt.clicked.connect(self.add_travel)
        self.close_bnt = QPushButton("关闭(ESC)")
        self.close_bnt.setStyleSheet("width:40px;height:30px;color:white;background-color:red;border:hidden;")
        self.close_bnt.clicked.connect(self.close_win)

        bnt_widget = QWidget()
        bnt_layout = QHBoxLayout()
        bnt_layout.addWidget(self.close_bnt)
        bnt_layout.addWidget(new_travel_bnt)
        bnt_widget.setLayout(bnt_layout)

        layout = QVBoxLayout()
        layout.addWidget(self.table)
        # layout.addWidget(bnt_widget)
        layout.addWidget(self.close_bnt)
        self.setLayout(layout)

    def button_row(self, travel_uuid):
        self.widget = QWidget()
        travel_uuid = travel_uuid
        self.view_bnt = QPushButton('查看')
        self.del_bnt = QPushButton('删除')

        self.view_bnt.setStyleSheet('''text-align:center; background-color:green;border-style:outset;height:20px;color:white;''')
        self.del_bnt.setStyleSheet('''text-align:center;background-color: red;border-style:outset;height:20px;color:white;''')

        self.hlayout = QHBoxLayout()

        # self.hlayout.addWidget(self.view_bnt)
        self.hlayout.addWidget(self.del_bnt)
        self.hlayout.setContentsMargins(5,2,5,2)

        self.view_bnt.clicked.connect(lambda:self.view_record(travel_uuid))
        self.del_bnt.clicked.connect(lambda:self.del_record(travel_uuid))
        self.widget.setLayout(self.hlayout)
        return self.widget

    def view_record(self,travel_uuid):
        if self.win:
            self.win.close()
        self.win = Travel(travel_uuid)
        self.cur.execute('select * from travel where travel_uuid = "{}"'.format(travel_uuid))
        rslt = self.cur.fetchone()
        self.win.show()
        self.win.uuid = self.uuid
        self.win.travel_uuid.setText(travel_uuid)
        self.win.begin_date.setDateTime(self.to_pydatetime(rslt[2]))
        self.win.from_address.setText(rslt[3])
        self.win.from_gps.setText(rslt[4])
        self.win.end_date.setDateTime(self.to_pydatetime(rslt[5]))
        self.win.to_address.setText(rslt[6])
        self.win.to_gps.setText(rslt[7])
        self.win.vehicle.setText(rslt[8])
        self.win.co_traveller.setText(rslt[9])
        self.win.backup.setText(rslt[10])
        self.win.save_bnt.setText('更新')
        self.win.new_bnt.setVisible(False)
        self.win.close_bnt.clicked.connect(self.refresh)

    def del_record(self,address):
        sql = ''' delete from travel where uuid = "{}" and address = "{}"
            '''.format(self.uuid,address)
        msg = QMessageBox.warning(self,'tips','是否删除',QMessageBox.Yes,QMessageBox.No)
        if msg == QMessageBox.Yes:
            self.cur.execute(sql)
            self.db.con.commit()
            self.refresh()

    def to_date(self,a):
        b = datetime.datetime(1970, 1, 1) + datetime.timedelta(seconds=a)
        c = b.strftime('%Y-%m-%d')
        return c

    def to_pydatetime(self, a):
        b = datetime.datetime(1970, 1, 1, 0, 0) + datetime.timedelta(seconds=a)
        return QDateTime(b.year, b.month, b.day, b.hour, b.minute)

    def add_travel(self):
        if self.win:
            self.win.close()
        self.win = Travel(self.uuid)
        self.win.show()
        self.win.close_bnt.clicked.connect(self.refresh)

    def close_win(self):
        self.db.con.close()
        self.close()

    def refresh(self):
        self.close()
        self.win = Travel_History(self.uuid)
        self.win.show()

    def center(self):
        self.screen = QDesktopWidget().screenGeometry()
        self.size = self.geometry()
        self.move(int((self.screen.width()-self.size.width())/2),
                 int((self.screen.height()-self.size.height())/2)
                  )

    def keyPressEvent(self,e):
        if e.key() == Qt.Key_Escape:
            self.close_win()



class Location(QWidget):

    def __init__(self,uuid):
        super(Location, self).__init__()
        self.setWindowTitle("旅居史")
        self.setFixedSize(300,420)
        self.setWindowFlags(Qt.WindowMinimizeButtonHint)
        self.center()
        self.db = DB()
        self.cur = self.db.cur
        self.uuid = uuid
        self.set_ui()

    def set_ui(self):
        self.save_bnt = QPushButton("保存(F1)")
        self.save_bnt.clicked.connect(self.save_record)
        self.save_bnt.setStyleSheet("width:40px;height:30px;color:white;background-color:green;border:hidden;")
        self.new_bnt = QPushButton("添加(F2)")
        self.new_bnt.clicked.connect(self.new_record)
        self.new_bnt.setStyleSheet("width:40px;height:30px;color:white;background-color:brown;border:hidden;")
        self.close_bnt = QPushButton("关闭(ESC)")
        self.close_bnt.clicked.connect(self.close_win)
        self.close_bnt.setStyleSheet("width:40px;height:30px;color:white;background-color:red;border:hidden;")
        self.history_bnt = QPushButton("旅居史")
        self.history_bnt.clicked.connect(self.show_history)
        self.history_bnt.setStyleSheet("width:40px;height:30px;color:white;background-color:blue;border:hidden;")
        bnt_widget = QWidget()
        bnt_layout = QHBoxLayout()
        bnt_layout.addWidget(self.close_bnt)
        # bnt_layout.addWidget(self.history_bnt)
        bnt_layout.addWidget(self.new_bnt)
        bnt_layout.addWidget(self.save_bnt)
        bnt_widget.setLayout(bnt_layout)

        self.begin_date_label = QLabel("开始日期")
        self.begin_date = QDateTimeEdit()
        self.begin_date.setStyleSheet("width:60px;height:30px;color:black;background-color:white;border:hidden;")
        self.begin_date.setCalendarPopup(True)
        self.begin_date.setDateTime(QDateTime.currentDateTime())
        self.begin_date_bnt = QPushButton()
        self.begin_date_bnt.setStyleSheet("border:hidden;")
        self.begin_date_bnt.setIcon(QIcon('./img/cal.ico'))
        self.begin_date_bnt.clicked.connect(lambda:self.show_dt_cal(self.begin_date))
        self.end_date_label = QLabel("离开日期")
        self.end_date = QDateTimeEdit()
        self.end_date.setCalendarPopup(True)
        self.end_date.setStyleSheet("width:60px;height:30px;color:black;background-color:white;border:hidden;")
        self.end_date.setDateTime(QDateTime.currentDateTime())
        self.end_date_bnt = QPushButton()
        self.end_date_bnt.setStyleSheet("border:hidden;")
        self.end_date_bnt.setIcon(QIcon('./img/cal.ico'))
        self.end_date_bnt.clicked.connect(lambda:self.show_dt_cal(self.end_date))

        self.from_address = QLineEdit()
        self.from_address.setStyleSheet("width:60px;height:30px;color:black;background-color:white;border:hidden;")
        self.from_gps = QLineEdit()
        self.from_gps.setStyleSheet("width:60px;height:30px;color:black;background-color:white;border:hidden;")

        # self.to_address = QLineEdit()
        # self.to_gps = QLineEdit()
        # self.to_address.setStyleSheet("width:60px;height:30px;color:black;background-color:white;border:hidden;")
        # self.to_gps.setStyleSheet("width:60px;height:30px;color:black;background-color:white;border:hidden;")

        # self.vehicle = QLineEdit()
        # self.vehicle.setStyleSheet("width:60px;height:30px;color:black;background-color:white;border:hidden;")

        self.co_traveller = QLineEdit()
        self.co_traveller.setStyleSheet("width:60px;height:30px;color:black;background-color:white;border:hidden;")
        self.backup = QLineEdit()
        self.backup.setStyleSheet("width:60px;height:30px;color:black;background-color:white;border:hidden;")

        self.travel_uuid = QLabel(self.gen_uuid())
        self.travel_uuid.setStyleSheet("width:60px;height:30px;color:black;background-color:None;border:hidden;")

        layout = QGridLayout()
        # layout.addWidget(QLabel("+++++旅行史+++++"),0,0,1,2)
        layout.addWidget(QLabel("地点"),1,0)
        layout.addWidget(self.from_address,1,1)
        layout.addWidget(QLabel("GPS"),2,0)
        layout.addWidget(self.from_gps,2,1)
        layout.addWidget(self.begin_date_label,3,0)
        layout.addWidget(self.begin_date,3,1)
        layout.addWidget(self.begin_date_bnt,3,2)
        # layout.addWidget(QLabel("目的地"),4,0)
        # layout.addWidget(self.to_address,4,1)
        # layout.addWidget(QLabel("目的地GPS"),5,0)
        # layout.addWidget(self.to_gps,5,1)
        layout.addWidget(self.end_date_label,6,0)
        layout.addWidget(self.end_date,6,1)
        layout.addWidget(self.end_date_bnt,6,2)
        # layout.addWidget(QLabel("交通工具"),7,0)
        # layout.addWidget(self.vehicle, 7, 1)
        layout.addWidget(QLabel("同行者"),8,0)
        layout.addWidget(self.co_traveller,8,1)
        layout.addWidget(QLabel("备注"),9,0)
        layout.addWidget(self.backup,9,1)

        layout.addWidget(bnt_widget,10,0,1,3)
        layout.addWidget(self.travel_uuid,12,0,1,3)
        self.setLayout(layout)

    def save_record(self):
        data_dic = {
            'uuid':self.uuid,
            'date_from':self.change_datetime(self.begin_date),
            'address':self.from_address.text(),
            'gps':self.from_gps.text(),
            'date_to':self.change_datetime(self.end_date),
            'co_loc': self.co_traveller.text(),
            'backup': self.backup.text(),
            'loc_uuid':self.travel_uuid.text(),
        }
        if self.from_address.text():
            if self.save_bnt.text() == '保存(F1)':
            # try:
                sql = '''insert into location values ("{uuid}","{address}",{date_from},
                        {date_to},"{gps}",
                        "{co_loc}","{backup}",'{loc_uuid}')
                    '''.format(**data_dic)
                self.save_bnt.setText("更新")
            else:
            # except:
                sql = '''update location set date_from = {date_from},
                        address = "{address}",gps= "{gps}",date_to = {date_to},
                        co_loc = "{co_loc}",backup = "{backup}" where
                        loc_uuid = "{loc_uuid}"
                    '''.format(**data_dic)
            self.cur.execute(sql)
            self.db.con.commit()

    def new_record(self):
        self.travel_uuid.setText(self.gen_uuid())
        self.save_bnt.setText('保存(F1)')
        self.from_address.clear()
        self.from_gps.clear()
        # self.to_address.clear()
        # self.to_gps.clear()
        # self.vehicle.clear()
        self.co_traveller.clear()
        self.backup.clear()

    def close_win(self):
        self.db.con.close()
        self.close()

    def show_dt_cal(self, tmp):#含时间日历控件
        self.cal = Calendar_With_Clock(tmp)
        self.cal.show()
        self.cal.datetime_signal.connect(self.input_deathday)

    def input_deathday(self, list):
        list[0].setDateTime(list[1])

    def change_datetime(self,pyqtdatetime):   # a pyqtdate style /yyyymmdd to time stamp
        pydate = pyqtdatetime.dateTime().toPyDateTime()
        base_date = datetime.datetime(1970,1,1,0,0)
        day_delta = pydate - base_date
        days = day_delta.days
        seconds = day_delta.seconds
        seconds += days*24*3600
        return seconds

    def show_history(self):
        self.win = Location_History(self.uuid)
        self.win.show()

    def gen_uuid(self):
        return (str(uuid.uuid1()))

    def center(self):
        self.screen = QDesktopWidget().screenGeometry()
        self.size = self.geometry()
        self.move(int((self.screen.width()-self.size.width())/2),
                 int((self.screen.height()-self.size.height())/2)
                  )

    def keyPressEvent(self,e):
        if e.key() == Qt.Key_Escape:
            self.close_win()
        if e.key() == Qt.Key_F1:
            self.save_record()
        if e.key() == Qt.Key_F2:
            self.new_record()

class Location_History(QWidget):

    def __init__(self,uuid):
        super(Location_History,self).__init__()
        self.uuid = uuid
        self.setWindowTitle("旅居史一览表")
        self.setFixedSize(800,500)
        self.setWindowFlags(Qt.WindowMinimizeButtonHint)
        self.center()
        self.db = DB()
        self.cur = self.db.cur
        self.cur.execute('select * from location where uuid = "{}" order by date'.format(self.uuid))
        self.rslt = self.cur.fetchall()
        self.win = None
        self.set_ui()

    def set_ui(self):

        count = len(self.rslt)
        self.table = QTableWidget(count,7) #15rows 15columns
        table_width_list = [100,120,100,120,100,150,150]
        for i in range(7):
            self.table.setColumnWidth(i,table_width_list[i])

        self.table.setHorizontalHeaderLabels(['操作','地点','出发时间','离开时间','gps','同行','备注'])
        k = 0  # k 行数
        for i in self.rslt:
            if i:
                for j in range(1,7): #15columns 第一列为操作列
                    if j in [2,3]:
                        item_tmp = QTableWidgetItem(self.to_date(i[j]))
                    else:
                        item_tmp = QTableWidgetItem(str(i[j]))
                    item_tmp.setTextAlignment(Qt.AlignCenter)
                    self.table.setItem(k,j,item_tmp)
                self.table.setCellWidget(k,0,self.button_row(i[-1]))
            else:
                break
            k += 1

        new_travel_bnt = QPushButton("添加行程")
        new_travel_bnt.setStyleSheet("width:40px;height:30px;color:white;background-color:green;border:hidden;")
        new_travel_bnt.clicked.connect(self.add_travel)
        self.close_bnt = QPushButton("关闭(ESC)")
        self.close_bnt.setStyleSheet("width:40px;height:30px;color:white;background-color:red;border:hidden;")
        self.close_bnt.clicked.connect(self.close_win)

        bnt_widget = QWidget()
        bnt_layout = QHBoxLayout()
        bnt_layout.addWidget(self.close_bnt)
        bnt_layout.addWidget(new_travel_bnt)
        bnt_widget.setLayout(bnt_layout)

        layout = QVBoxLayout()
        layout.addWidget(self.table)
        layout.addWidget(bnt_widget)
        self.setLayout(layout)

    def button_row(self, travel_uuid):
        self.widget = QWidget()
        travel_uuid = travel_uuid
        self.view_bnt = QPushButton('查看')
        self.del_bnt = QPushButton('删除')

        self.view_bnt.setStyleSheet('''text-align:center; background-color:green;border-style:outset;height:20px;color:white;''')
        self.del_bnt.setStyleSheet('''text-align:center;background-color: red;border-style:outset;height:20px;color:white;''')

        self.hlayout = QHBoxLayout()

        self.hlayout.addWidget(self.view_bnt)
        self.hlayout.addWidget(self.del_bnt)
        self.hlayout.setContentsMargins(5,2,5,2)

        self.view_bnt.clicked.connect(lambda:self.view_record(travel_uuid))
        self.del_bnt.clicked.connect(lambda:self.del_record(travel_uuid))
        self.widget.setLayout(self.hlayout)
        return self.widget

    def view_record(self,travel_uuid):
        if self.win:
            self.win.close()
        self.win = Location(travel_uuid)
        self.cur.execute('select * from location where loc_uuid = "{}"'.format(travel_uuid))
        rslt = self.cur.fetchone()
        self.win.show()
        self.win.uuid = self.uuid
        self.win.travel_uuid.setText(travel_uuid)
        self.win.begin_date.setDateTime(self.to_pydatetime(rslt[2]))
        self.win.from_address.setText(rslt[1])
        self.win.from_gps.setText(rslt[4])
        self.win.end_date.setDateTime(self.to_pydatetime(rslt[3]))
        self.win.co_traveller.setText(rslt[5])
        self.win.backup.setText(rslt[6])
        self.win.save_bnt.setText('更新')
        self.win.new_bnt.setVisible(False)
        self.win.close_bnt.clicked.connect(self.refresh)

    def del_record(self,travel_uuid):
        sql = ''' delete from location where travel_uuid = "{}"
            '''.format(travel_uuid)
        msg = QMessageBox.warning(self,'tips','是否删除',QMessageBox.Yes,QMessageBox.No)
        if msg == QMessageBox.Yes:
            self.cur.execute(sql)
            self.db.con.commit()
            self.refresh()

    def to_date(self,a):
        b = datetime.datetime(1970, 1, 1) + datetime.timedelta(seconds=a)
        c = b.strftime('%m/%d %H:%M')
        return c

    def to_pydatetime(self, a):
        b = datetime.datetime(1970, 1, 1, 0, 0) + datetime.timedelta(seconds=a)
        return QDateTime(b.year, b.month, b.day, b.hour, b.minute)

    def add_travel(self):
        if self.win:
            self.win.close()
        self.win = Location(self.uuid)
        self.win.show()
        self.win.close_bnt.clicked.connect(self.refresh)

    def close_win(self):
        self.db.con.close()
        self.close()

    def refresh(self):
        self.close()
        self.win = Location_History(self.uuid)
        self.win.show()

    def center(self):
        self.screen = QDesktopWidget().screenGeometry()
        self.size = self.geometry()
        self.move(int((self.screen.width()-self.size.width())/2),
                 int((self.screen.height()-self.size.height())/2)
                  )

    def keyPressEvent(self,e):
        if e.key() == Qt.Key_Escape:
            self.close_win()
